package com.ykq.gateway.filter;

import com.alibaba.fastjson.JSON;
import com.ykq.gateway.entity.AnonUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: education-parent
 * @description:
 * @author: 闫克起2
 * @create: 2022-08-04 09:07
 **/
@Component
public class LoginFilter implements GlobalFilter, Ordered {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AnonUrl anonUrl;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //查看你的请求路径是否为/sso/login
        String path = request.getPath().toString();
        if(!StringUtils.isEmpty(path)&&path.contains("/sso/login")){
            return chain.filter(exchange);
        }
        //获取请求头 token
        String token = request.getHeaders().getFirst("token");
        //表示用户登录了。
        if(!StringUtils.isEmpty(token)&&redisTemplate.hasKey(token)){
            //只要登录则可以直接放行的路径
            if(anonUrl.getUrl().contains(path)){
                return chain.filter(exchange);
            }
            ValueOperations forValue = redisTemplate.opsForValue();
            String json = (String) forValue.get(token);

            //把json字符串转换为java对象
            Map map = JSON.parseObject(json, Map.class);
            //获取登录者具有的权限
            List<String> permissions = (List<String>) map.get("permissions");
            if(permissions.contains(path)){
                return chain.filter(exchange);
            }else{
                return result(response,"权限不足",4001);
            }
        }
        return result(response,"未登录",4002);
    }

    private Mono<Void> result(ServerHttpResponse response,String msg,Integer code){
        //3.1设置状态码
        response.setStatusCode(HttpStatus.UNAUTHORIZED);
        //3.2封装返回数据
        Map<String, Object> map = new HashMap<>();
        map.put("msg", msg);
        map.put("code", code);

        //3.3作JSON转换
        byte[] bytes = JSON.toJSONString(map).getBytes(StandardCharsets.UTF_8);

        //3.4调用bufferFactory方法,生成DataBuffer对象
        DataBuffer buffer = response.bufferFactory().wrap(bytes);

        //4.调用Mono中的just方法,返回要写给前端的JSON数据
        return response.writeWith(Mono.just(buffer));
    }

    @Override
    public int getOrder() {
        return 0;
    }
}




















